home *** CD-ROM | disk | FTP | other *** search
- class CTrayBuilder
- {
- var _arrGroups;
- var _desiredGroupSize;
- var _minGroupSize;
- var _maxGroupSize;
- var _tileGrid;
- var _trayExtentX = 0;
- var _trayExtentY = 0;
- function CTrayBuilder()
- {
- }
- function Build(trayExtentX, trayExtentY, minGroupSize, maxGroupSize)
- {
- this._arrGroups = new Array();
- this._desiredGroupSize = new Array();
- if(minGroupSize == undefined)
- {
- minGroupSize = 1;
- }
- if(maxGroupSize == undefined)
- {
- maxGroupSize = 6;
- }
- this._minGroupSize = minGroupSize;
- this._maxGroupSize = maxGroupSize;
- this._trayExtentX = trayExtentX;
- this._trayExtentY = trayExtentY;
- this.RandomizeTray();
- return this._arrGroups;
- }
- function RandomizeTray()
- {
- this._tileGrid = new Array();
- var _loc3_ = 0;
- while(_loc3_ < this._trayExtentY)
- {
- var _loc2_ = 0;
- while(_loc2_ < this._trayExtentX)
- {
- this._tileGrid[_loc3_ * this._trayExtentX + _loc2_] = -1;
- _loc2_ = _loc2_ + 1;
- }
- _loc3_ = _loc3_ + 1;
- }
- this.CreatePieces();
- }
- function CreateNewGroup()
- {
- this._arrGroups.push(new Array());
- return this._arrGroups.length - 1;
- }
- function CreatePieces()
- {
- FreshDebug.Assert(this._tileGrid != undefined,"_tileGrid != undefined");
- var _loc4_ = 0;
- while(_loc4_ < this._trayExtentY)
- {
- var _loc2_ = 0;
- while(_loc2_ < this._trayExtentX)
- {
- if(this._tileGrid[_loc4_ * this._trayExtentX + _loc2_] < 0)
- {
- var _loc3_ = this.CreateNewGroup();
- FreshDebug.Assert(_loc3_ >= 0,"iGroup >= 0");
- this.EstablishGroup(new Vector2D(_loc2_,_loc4_),_loc3_);
- }
- _loc2_ = _loc2_ + 1;
- }
- _loc4_ = _loc4_ + 1;
- }
- }
- function CompareNeighborsRandomly(v1, v2)
- {
- return !_root.random.GetBoolean() ? 1 : -1;
- }
- function AddToGroup(tile, iGroup)
- {
- this._tileGrid[tile._y * this._trayExtentX + tile._x] = iGroup;
- this._arrGroups[iGroup].push(tile);
- if(this.GetGroupSize(iGroup) >= this.GetDesiredGroupSize(iGroup))
- {
- return undefined;
- }
- var _loc5_ = new Array();
- _loc5_.push(new Vector2D(tile._x + 1,tile._y + 0));
- _loc5_.push(new Vector2D(tile._x - 1,tile._y + 0));
- _loc5_.push(new Vector2D(tile._x + 0,tile._y + 1));
- _loc5_.push(new Vector2D(tile._x + 0,tile._y - 1));
- _loc5_.sort(this.CompareNeighborsRandomly);
- var _loc3_ = 0;
- while(_loc3_ < _loc5_.length)
- {
- var _loc2_ = _loc5_[_loc3_];
- if(!(_loc2_._x < 0 || _loc2_._y < 0 || _loc2_._x >= this._trayExtentX || _loc2_._y >= this._trayExtentY))
- {
- if(this._tileGrid[_loc2_._y * this._trayExtentX + _loc2_._x] < 0)
- {
- this.AddToGroup(_loc2_,iGroup);
- if(this.GetGroupSize(iGroup) >= this.GetDesiredGroupSize(iGroup))
- {
- return undefined;
- }
- }
- }
- _loc3_ = _loc3_ + 1;
- }
- }
- function PickRandomDesiredGroupSize(iGroup)
- {
- var _loc3_ = _root.random.GetRandom();
- Math.pow(_loc3_,5);
- this._desiredGroupSize[iGroup] = this._minGroupSize + _loc3_ * (this._maxGroupSize - this._minGroupSize);
- }
- function GetGroupSize(iGroup)
- {
- return this._arrGroups[iGroup].length;
- }
- function GetDesiredGroupSize(iGroup)
- {
- return this._desiredGroupSize[iGroup];
- }
- function EstablishGroup(startTile, iGroup)
- {
- this.PickRandomDesiredGroupSize(iGroup);
- this.AddToGroup(startTile,iGroup);
- }
- }
-